//
//  GlobalData.swift
//  全局数据
//
//  Created by mini on 2025/4/28.
//

import Foundation
import UIKit

var GB_Nav: UINavigationController?

class GlobalData: NSObject {
    var statusHidden: Bool = false // 状态栏隐藏状态
    var statusBarStyle: UIStatusBarStyle = .default // 状态栏类型

    private var _GB_SessionID: String?
    private var _GB_UserModel: ModelUser?
    
    
    // MARK: - 单例
    static let sharedInstance = GlobalData()

    override private init() {}

    // 用户 _GB_SessionID
    var GB_SessionID: String? {
        get {
            if _GB_SessionID == nil || _GB_SessionID?.isEmpty == true {
                _GB_SessionID = GlobalMethod.readStrFromUserDefaults(forKey: LOCAL_SESSIONID)
            }
            return _GB_SessionID
        }
        set {
            let value = newValue ?? ""
            GlobalMethod.writeStr(value, forKey: LOCAL_SESSIONID)
            _GB_SessionID = newValue
        }
    }

    // 用户信息 GB_UserModel
    var GB_UserModel: ModelUser? {
        get {
            if (_GB_UserModel?.places) == nil || !isStr(_GB_UserModel?.places){
                let jsonString = GlobalMethod.readStrFromUserDefaults(forKey: LOCAL_USERMODEL)
                let dicItem = GlobalMethod.exchangeStringToDic(jsonString)
                _GB_UserModel = ModelUser.deserialize(from: dicItem)
            }
            return _GB_UserModel
        }
        set {
            _GB_UserModel = newValue
            if let userModel = newValue {
                // 直接使用 HandyJSON 的 toJSONString() 方法
                if let jsonString = userModel.toJSONString() {
                    GlobalMethod.writeStr(jsonString, forKey: LOCAL_USERMODEL)
                    // 发送通知
                    NotificationCenter.default.post(name: Notification.Name(NOTICE_USER_INFO_CHANGE), object: nil)
                }
            }
        }
    }
    
    
    
    

    lazy var GB_NoticeView: NoticeView = {
        let result = NoticeView()
        return result
    }()
}
